﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单.service;
using DXApplicationTangche.service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.门店下单.form.订单修改;
using DXApplicationTangche.UC.款式异常;
using mendian;
using DXApplicationTangche.UC.门店下单.DTO;
using DiaoPaiDaYin;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;
using DXApplicationTangche.UC.销售管理;
using DXApplicationTangche.UC.订单一览新.订单预览新;
using DXApplicationTangche.UC.样品管理.样品订单预览;
using DXApplicationTangche.UC.生产计划.生产批生产.service;
using DXApplicationTangche.UC.订单;
using DXApplicationTangche.UC.货号管理.service;
using DXApplicationTangche.UC.货号管理.销售款式;
using DXApplicationTangche.UC.坚山;
using DXApplicationTangche.model;
using DXApplicationTangche.UC.金蝶;

namespace DXApplicationTangche.UC.ImpUC
{
    public partial class XtraUserControl订单一览 : DevExpress.XtraEditors.XtraUserControl
    {
        private String orderId = "";
        private String shopId = "18";
        private String pageStart;
        private String pageSize;
        private Enum订单类别 enum订单类别 = Enum订单类别.销售订单;
        public delegate void AddTabDataEvents(Form form);//定义委托
        public event AddTabDataEvents addTabDataEvents;//定义事件
        public XtraUserControl订单一览()
        {
            InitializeComponent();
            this.xtraUserControl分页.williamPagerEvent += this.refreshGrid;
            this.judgeShop();
            this.refreshGrid(1);
            //this.bandedGridView1.ExpandAllGroups();
        }

        public XtraUserControl订单一览(Enum订单类别 @enum)
        {
            InitializeComponent();
            this.xtraUserControl分页.williamPagerEvent += this.refreshGrid;
            this.enum订单类别 = @enum;
            this.judgeShop();
            this.refreshGrid(1);
            this.bandedGridView1.ExpandAllGroups();
        }
        public XtraUserControl订单一览(String shopId)
        {
            InitializeComponent();
            this.xtraUserControl分页.williamPagerEvent += this.refreshGrid;
            //this.enum订单类别 = @enum;
            this.shopId = shopId;
            this.refreshGrid(1);
            this.bandedGridView1.ExpandAllGroups();
            this.bandedGridView1.OptionsFind.AlwaysVisible = false;
            this.bandedGridView1.OptionsView.ShowGroupPanel = false;
        }

        private void bandedGridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumnOrderStatusName"))
            {
                if ("未排产".Equals(e.CellValue))
                {
                    e.Appearance.BackColor = Color.Red;
                    e.Appearance.ForeColor = Color.White;
                }
                else if ("已排产".Equals(e.CellValue))
                {
                    e.Appearance.BackColor = Color.CadetBlue;
                    e.Appearance.ForeColor = Color.White;
                }
                else if ("在制品".Equals(e.CellValue))
                {
                    e.Appearance.BackColor = Color.Green;
                    e.Appearance.ForeColor = Color.White;
                }
                else if ("完成".Equals(e.CellValue))
                {
                    e.Appearance.BackColor = Color.Purple;
                    e.Appearance.ForeColor = Color.White;
                }
                else if ("取消".Equals(e.CellValue))
                {
                    e.Appearance.BackColor = Color.Yellow;
                    e.Appearance.ForeColor = Color.Black;
                }
            }
        }

        private void refreshGrid(int curPage)
        {
            //从DTO获取数据，绑定数据
            //API只要一行，原子性
            this.gridControl1.DataSource = new 订单Service(this.shopId, (curPage - 1).ToString(), "1000").orderDTOs;
            this.bandedGridView1.ExpandAllGroups();
        }

        private void judgeShop()
        {
            switch (this.enum订单类别)
            {
                case Enum订单类别.销售订单:
                    this.shopId = "18";
                    break;
                case Enum订单类别.样品订单:
                    this.shopId = "48";
                    this.查看成衣码ToolStripMenuItem.Visible = true;
                    break;
                case Enum订单类别.套号订单:
                    this.shopId = "50";
                    break;
                case Enum订单类别.坚山订单:
                    this.shopId = "46";
                    break;
                case Enum订单类别.坚山美国订单:
                    this.shopId = "49";
                    break;
                case Enum订单类别.Luna店铺订单:
                    this.shopId = "51";
                    break;
                case Enum订单类别.坚山上海订单:
                    this.shopId = "52";
                    break;
            }
        }

        private void bandedGridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            this.orderId = this.bandedGridView1.GetRowCellValue(e.RowHandle, "orderId").ToString();
            if (this.enum订单类别 == Enum订单类别.销售订单)
            {
                //new Form订单预览新(this.orderId).Show();
                try { this.addTabDataEvents(new Form订单预览新(this.orderId)); } catch { }
            }
            else if (this.enum订单类别 == Enum订单类别.样品订单)
            {
                new Form样品订单预览新(this.orderId).ShowDialog();
            }
            else
            {
                new Form订单预览新(this.orderId).ShowDialog();
            }
        }

        private void 取消订单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "orderId").ToString();
                String orderStatusName = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "orderStatusName").ToString();
                if (!"取消".Equals(orderStatusName))
                {
                    MessageBoxButtons messBtn = MessageBoxButtons.OKCancel;
                    DialogResult dr = MessageBox.Show("确定取消订单？", "提示:确认", messBtn);
                    if (dr == DialogResult.OK)
                    {
                        RestCall.PostHttp(URL.接口url + "/pmc/orderCancel", orderId);
                        MessageBox.Show("已改为取消订单");
                        this.refreshGrid(1);
                    }
                }
                else
                {
                    MessageBox.Show("已取消，不能重复取消订单");
                }
            }
            catch
            {
                MessageBox.Show("修改订单状态失败");
            }
        }

        private void 查看成衣码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId
                    = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "orderId").ToString();
                String barcode = 生产批生产Service.saleInitWIPBatchAndWarterMarkPrintLog(orderId);
                new Form查看成衣水洗码(barcode).ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 坚山款式ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String refStyleId = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "refStyleId").ToString();
            String styleNameCn = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "styleNameCn").ToString();
            new Form坚山新增款式(refStyleId, styleNameCn).ShowDialog();
        }

        private void 查看订单BOMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String orderId
                    = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "orderId").ToString();
                new Form订单对应BOM(orderId).ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void 维护BOMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                String styleNameCn = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "styleNameCn").ToString();
                String materialCode = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "materialCode").ToString();
                new Form金蝶EBOM修改(styleNameCn, materialCode).ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
